package com.android.equipment.ui.group

import android.content.Intent
import android.os.Bundle
import com.android.equipment.R
import com.android.equipment.adapter.PeopleGroupAdapter
import com.android.equipment.base.BaseActivity
import com.android.equipment.dao.TeamGroup
import com.android.equipment.dao.operate.DaoTeamGroup
import com.android.equipment.dao.operate.DaoTeamGroupDetail
import com.android.equipment.databinding.ActivityPeopleGroupBinding
import com.gyf.immersionbar.ImmersionBar
import com.lxj.xpopup.XPopup

class TeamGroupActivity: BaseActivity<ActivityPeopleGroupBinding>() {
    val adapter : PeopleGroupAdapter by lazy {
        PeopleGroupAdapter(R.layout.item_people_group)
    }

    private lateinit var peopleGroupList : List<TeamGroup>

    override fun setLayoutContent() = ActivityPeopleGroupBinding.inflate(layoutInflater)

    override fun initView(savedInstanceState: Bundle?) {
        initRecycler()
    }

    override fun initData() {

    }

    override fun onResume() {
        super.onResume()
        refreshData()
    }

    private fun initRecycler(){
        binding.rvPeopleGroup.adapter = adapter
        adapter.setOnItemClickListener { _, _, position ->
            val intent = Intent(mContext, EditTeamGroupActivity::class.java)
            intent.putExtra("id",adapter.getItem(position).id)
            mSwipeBackHelper.forward(intent)
        }

        adapter.setOnItemLongClickListener { _, _, position ->
            XPopup.Builder(mContext).asConfirm("是否删除该班组？", "") {
                // 先删除该班组的详情数据
                DaoTeamGroupDetail.getInstance().deleteTeamGroupDetailByGroupId(adapter.getItem(position).id)
                DaoTeamGroup.getInstance().deleteTeamGroup(adapter.getItem(position))
                refreshData()
            }.show()
            true
        }
    }

    override fun initEvent() {
        binding.mTitleBar.leftView.setOnClickListener {
            mSwipeBackHelper.backward()
        }

        binding.mTitleBar.rightView.setOnClickListener {
            mSwipeBackHelper.forward(EditTeamGroupActivity::class.java)
        }
    }

    private fun refreshData() {
        peopleGroupList = DaoTeamGroup.getInstance().allTeamGroup
        peopleGroupList.let {
            adapter.setList(it)
        }


    }

    override fun setStatusBar() {
        ImmersionBar.with(this)
            .fullScreen(false)
            .statusBarColor(R.color.F8F9FA)
            .statusBarDarkFont(true)
            .titleBar(binding.mTitleBar)
            .init()
    }
}